home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / expread.y < prev    next >
Encoding:
Text File  |  1992-03-25  |  27.6 KB  |  1,237 lines

  1. /* Parse C expressions for GDB.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute GDB,
  11. but only under the conditions described in the GDB General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with GDB so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17. In other words, go ahead and share GDB, but don't try to stop
  18. anyone else from sharing it farther.  Help stamp out software hoarding!
  19. */
  20.  
  21. /* Parse a C expression from text in a string,
  22.    and return the result as a  struct expression  pointer.
  23.    That structure contains arithmetic operations in reverse polish,
  24.    with constants represented by operations that are followed by special data.
  25.    See expression.h for the details of the format.
  26.    What is important here is that it can be built up sequentially
  27.    during the process of parsing; the lower levels of the tree always
  28.    come first in the result.  */
  29.    
  30. %{
  31. #include "defs.h"
  32. #include "param.h"
  33. #include "symtab.h"
  34. #include "frame.h"
  35. #include "expression.h"
  36.  
  37. #include <stdio.h>
  38.  
  39. #ifdef atarist
  40. extern int gcc_mshort;
  41. #endif
  42.  
  43. extern CORE_ADDR end_of_text_addr;
  44. static struct expression *expout;
  45. static int expout_size;
  46. static int expout_ptr;
  47.  
  48. static int yylex ();
  49. static yyerror ();
  50. static void write_exp_elt ();
  51. static void write_exp_string ();
  52. static void start_arglist ();
  53. static int end_arglist ();
  54. static void free_funcalls ();
  55. static char *copy_name ();
  56.  
  57. /* If this is nonzero, this block is used as the lexical context
  58.    for symbol names.  */
  59.  
  60. static struct block *expression_context_block;
  61.  
  62. /* Number of arguments seen so far in innermost function call.  */
  63. static int arglist_len;
  64.  
  65. /* Data structure for saving values of arglist_len
  66.    for function calls whose arguments contain other function calls.  */
  67.  
  68. struct funcall
  69.   {
  70.     struct funcall *next;
  71.     int arglist_len;
  72.   };
  73.  
  74. struct funcall *funcall_chain;
  75.  
  76. /* This kind of datum is used to represent the name
  77.    of a symbol token.  */
  78.  
  79. struct stoken
  80.   {
  81.     char *ptr;
  82.     int length;
  83.   };
  84. %}
  85.  
  86. /* Although the yacc "value" of an expression is not used,
  87.    since the result is stored in the structure being created,
  88.    other node types do have values.  */
  89.  
  90. %union
  91.   {
  92.     long lval;
  93.     double dval;
  94.     struct symbol *sym;
  95.     struct type *tval;
  96.     struct stoken sval;
  97.     int voidval;
  98.     struct block *bval;
  99.     enum exp_opcode opcode;
  100.     struct internalvar *ivar;
  101.   }
  102.  
  103. %type <voidval> exp exp1 start variable
  104. %type <tval> type typebase
  105. %type <bval> block
  106.  
  107. %token <lval> INT CHAR
  108. %token <dval> FLOAT
  109.  
  110. /* Both NAME and TYPENAME tokens represent symbols in the input,
  111.    and both convey their data as strings.
  112.    But a TYPENAME is a string that happens to be defined as a typedef
  113.    or builtin type name (such as int or char)
  114.    and a NAME is any other symbol.
  115.  
  116.    Contexts where this distinction is not important can use the
  117.    nonterminal "name", which matches either NAME or TYPENAME.  */
  118.  
  119. %token <sval> NAME TYPENAME STRING
  120. %type <sval> name
  121.  
  122. %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
  123.  
  124. %token <lval> LAST REGNAME
  125.  
  126. %token <ivar> VARIABLE
  127.  
  128. %token <opcode> ASSIGN_MODIFY
  129.  
  130. %left ','
  131. %left ABOVE_COMMA
  132. %right '=' ASSIGN_MODIFY
  133. %right '?'
  134. %left OR
  135. %left AND
  136. %left '|'
  137. %left '^'
  138. %left '&'
  139. %left EQUAL NOTEQUAL
  140. %left '<' '>' LEQ GEQ
  141. %left LSH RSH
  142. %left '+' '-'
  143. %left '*' '/' '%'
  144. %left '@'
  145. %right UNARY INCREMENT DECREMENT
  146. %right ARROW '.' '['
  147. %left COLONCOLON
  148.  
  149. %%
  150.  
  151. start   :    exp1
  152.     ;
  153.  
  154. /* Expressions, including the comma operator.  */
  155. exp1    :    exp
  156.     |    exp1 ',' exp
  157.             { write_exp_elt (BINOP_COMMA); }
  158.     ;
  159.  
  160. /* Expressions, not including the comma operator.  */
  161. exp    :    '*' exp    %prec UNARY
  162.             { write_exp_elt (UNOP_IND); }
  163.  
  164. exp    :    '&' exp    %prec UNARY
  165.             { write_exp_elt (UNOP_ADDR); }
  166.  
  167. exp    :    '-' exp    %prec UNARY
  168.             { write_exp_elt (UNOP_NEG); }
  169.     ;
  170.  
  171. exp    :    '!' exp    %prec UNARY
  172.             { write_exp_elt (UNOP_ZEROP); }
  173.     ;
  174.  
  175. exp    :    '~' exp    %prec UNARY
  176.             { write_exp_elt (UNOP_LOGNOT); }
  177.     ;
  178.  
  179. exp    :    INCREMENT exp    %prec UNARY
  180.             { write_exp_elt (UNOP_PREINCREMENT); }
  181.     ;
  182.  
  183. exp    :    DECREMENT exp    %prec UNARY
  184.             { write_exp_elt (UNOP_PREDECREMENT); }
  185.     ;
  186.  
  187. exp    :    exp INCREMENT    %prec UNARY
  188.             { write_exp_elt (UNOP_POSTINCREMENT); }
  189.     ;
  190.  
  191. exp    :    exp DECREMENT    %prec UNARY
  192.             { write_exp_elt (UNOP_POSTDECREMENT); }
  193.     ;
  194.  
  195. exp    :    SIZEOF exp       %prec UNARY
  196.             { write_exp_elt (UNOP_SIZEOF); }
  197.     ;
  198.  
  199. exp    :    exp ARROW name
  200.             { write_exp_elt (STRUCTOP_PTR);
  201.               write_exp_string ($3);
  202.               write_exp_elt (STRUCTOP_PTR); }
  203.     ;
  204.  
  205. exp    :    exp '.' name
  206.             { write_exp_elt (STRUCTOP_STRUCT);
  207.               write_exp_string ($3);
  208.               write_exp_elt (STRUCTOP_STRUCT); }
  209.     ;
  210.  
  211. exp    :    exp '[' exp1 ']'
  212.             { write_exp_elt (BINOP_SUBSCRIPT); }
  213.     ;
  214.  
  215. exp    :    exp '(' 
  216.             /* This is to save the value of arglist_len
  217.                being accumulated by an outer function call.  */
  218.             { start_arglist (); }
  219.         arglist ')'    %prec ARROW
  220.             { write_exp_elt (OP_FUNCALL);
  221.               write_exp_elt (end_arglist ());
  222.               write_exp_elt (OP_FUNCALL); }
  223.     ;
  224.  
  225. arglist    :
  226.     ;
  227.  
  228. arglist    :    exp
  229.             { arglist_len = 1; }
  230.     ;
  231.  
  232. arglist    :    arglist ',' exp   %prec ABOVE_COMMA
  233.             { arglist_len++; }
  234.     ;
  235.  
  236. exp    :    '{' type '}' exp  %prec UNARY
  237.             { write_exp_elt (UNOP_MEMVAL);
  238.               write_exp_elt ($2);
  239.               write_exp_elt (UNOP_MEMVAL); }
  240.     ;
  241.  
  242. exp    :    '(' type ')' exp  %prec UNARY
  243.             { write_exp_elt (UNOP_CAST);
  244.               write_exp_elt ($2);
  245.               write_exp_elt (UNOP_CAST); }
  246.     ;
  247.  
  248. exp    :    '(' exp1 ')'
  249.             { }
  250.     ;
  251.  
  252. /* Binary operators in order of decreasing precedence.  */
  253.  
  254. exp    :    exp '@' exp
  255.             { write_exp_elt (BINOP_REPEAT); }
  256.     ;
  257.  
  258. exp    :    exp '*' exp
  259.             { write_exp_elt (BINOP_MUL); }
  260.     ;
  261.  
  262. exp    :    exp '/' exp
  263.             { write_exp_elt (BINOP_DIV); }
  264.     ;
  265.  
  266. exp    :    exp '%' exp
  267.             { write_exp_elt (BINOP_REM); }
  268.     ;
  269.  
  270. exp    :    exp '+' exp
  271.             { write_exp_elt (BINOP_ADD); }
  272.     ;
  273.  
  274. exp    :    exp '-' exp
  275.             { write_exp_elt (BINOP_SUB); }
  276.     ;
  277.  
  278. exp    :    exp LSH exp
  279.             { write_exp_elt (BINOP_LSH); }
  280.     ;
  281.  
  282. exp    :    exp RSH exp
  283.             { write_exp_elt (BINOP_RSH); }
  284.     ;
  285.  
  286. exp    :    exp EQUAL exp
  287.             { write_exp_elt (BINOP_EQUAL); }
  288.     ;
  289.  
  290. exp    :    exp NOTEQUAL exp
  291.             { write_exp_elt (BINOP_NOTEQUAL); }
  292.     ;
  293.  
  294. exp    :    exp LEQ exp
  295.             { write_exp_elt (BINOP_LEQ); }
  296.     ;
  297.  
  298. exp    :    exp GEQ exp
  299.             { write_exp_elt (BINOP_GEQ); }
  300.     ;
  301.  
  302. exp    :    exp '<' exp
  303.             { write_exp_elt (BINOP_LESS); }
  304.     ;
  305.  
  306. exp    :    exp '>' exp
  307.             { write_exp_elt (BINOP_GTR); }
  308.     ;
  309.  
  310. exp    :    exp '&' exp
  311.             { write_exp_elt (BINOP_LOGAND); }
  312.     ;
  313.  
  314. exp    :    exp '^' exp
  315.             { write_exp_elt (BINOP_LOGXOR); }
  316.     ;
  317.  
  318. exp    :    exp '|' exp
  319.             { write_exp_elt (BINOP_LOGIOR); }
  320.     ;
  321.  
  322. exp    :    exp AND exp
  323.             { write_exp_elt (BINOP_AND); }
  324.     ;
  325.  
  326. exp    :    exp OR exp
  327.             { write_exp_elt (BINOP_OR); }
  328.     ;
  329.  
  330. exp    :    exp '?' exp ':' exp    %prec '?'
  331.             { write_exp_elt (TERNOP_COND); }
  332.     ;
  333.               
  334. exp    :    exp '=' exp
  335.             { write_exp_elt (BINOP_ASSIGN); }
  336.     ;
  337.  
  338. exp    :    exp ASSIGN_MODIFY exp
  339.             { write_exp_elt (BINOP_ASSIGN_MODIFY);
  340.               write_exp_elt ($2);
  341.               write_exp_elt (BINOP_ASSIGN_MODIFY); }
  342.     ;
  343.  
  344. exp    :    INT
  345.             { write_exp_elt (OP_LONG);
  346. #if 0
  347.               if(gcc_mshort)
  348.                   write_exp_elt (builtin_type_short);
  349.               else
  350. #endif
  351.               write_exp_elt (builtin_type_int);
  352.               write_exp_elt ($1);
  353.               write_exp_elt (OP_LONG); }
  354.     ;
  355.  
  356. exp    :    CHAR
  357.             { write_exp_elt (OP_LONG);
  358.               write_exp_elt (builtin_type_char);
  359.               write_exp_elt ($1);
  360.               write_exp_elt (OP_LONG); }
  361.     ;
  362.  
  363. exp    :    FLOAT
  364.             { write_exp_elt (OP_DOUBLE);
  365.               write_exp_elt (builtin_type_double);
  366.               write_exp_elt ($1);
  367.               write_exp_elt (OP_DOUBLE); }
  368.     ;
  369.  
  370. exp    :    variable
  371.     ;
  372.  
  373. exp    :    LAST
  374.             { write_exp_elt (OP_LAST);
  375.               write_exp_elt ($1);
  376.               write_exp_elt (OP_LAST); }
  377.     ;
  378.  
  379. exp    :    REGNAME
  380.             { write_exp_elt (OP_REGISTER);
  381.               write_exp_elt ($1);
  382.               write_exp_elt (OP_REGISTER); }
  383.     ;
  384.  
  385. exp    :    VARIABLE
  386.             { write_exp_elt (OP_INTERNALVAR);
  387.               write_exp_elt ($1);
  388.               write_exp_elt (OP_INTERNALVAR); }
  389.     ;
  390.  
  391. exp    :    SIZEOF '(' type ')'    %prec UNARY
  392.             { write_exp_elt (OP_LONG);
  393.               write_exp_elt (builtin_type_long);
  394.               write_exp_elt ((long) TYPE_LENGTH ($3));
  395.               write_exp_elt (OP_LONG); }
  396.     ;
  397.  
  398. exp    :    STRING
  399.             { write_exp_elt (OP_STRING);
  400.               write_exp_string ($1);
  401.               write_exp_elt (OP_STRING); }
  402.     ;
  403.  
  404. block    :    name
  405.             { struct symtab *tem = lookup_symtab (copy_name ($1));
  406.               struct symbol *sym;
  407.               
  408.               if (tem)
  409.                 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
  410.               else
  411.                 {
  412.                   sym = lookup_symbol (copy_name ($1),
  413.                            expression_context_block,
  414.                            VAR_NAMESPACE);
  415.                   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  416.                 $$ = SYMBOL_BLOCK_VALUE (sym);
  417.                   else
  418.                 error ("No file or function \"%s\".",
  419.                        copy_name ($1));
  420.                 }}
  421.     ;
  422.  
  423. block    :    block COLONCOLON name
  424.             { struct symbol *tem
  425.                 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
  426.               if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  427.                 error ("No function \"%s\" in specified context.",
  428.                    copy_name ($3));
  429.               $$ = SYMBOL_BLOCK_VALUE (tem); }
  430.     ;
  431.  
  432. variable:    block COLONCOLON name
  433.             { struct symbol *sym;
  434.               sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
  435.               if (sym == 0)
  436.                 error ("No symbol \"%s\" in specified context.",
  437.                    copy_name ($3));
  438.               write_exp_elt (OP_VAR_VALUE);
  439.               write_exp_elt (sym);
  440.               write_exp_elt (OP_VAR_VALUE); }
  441.     ;
  442.  
  443. variable:    NAME
  444.             { struct symbol *sym;
  445.               sym = lookup_symbol (copy_name ($1),
  446.                           expression_context_block,
  447.                           VAR_NAMESPACE);
  448.               if (sym)
  449.                 {
  450.                   write_exp_elt (OP_VAR_VALUE);
  451.                   write_exp_elt (sym);
  452.                   write_exp_elt (OP_VAR_VALUE);
  453.                 }
  454.               else
  455.                 {
  456.                   register char *arg = copy_name ($1);
  457.                   register int i;
  458.                   for (i = 0; i < misc_function_count; i++)
  459.                 if (!strcmp (misc_function_vector[i].name, arg))
  460.                   break;
  461.  
  462.                   if (i < misc_function_count)
  463.                 {
  464.                   write_exp_elt (OP_LONG);
  465.                   write_exp_elt (builtin_type_int);
  466.                   write_exp_elt (misc_function_vector[i].address);
  467.                   write_exp_elt (OP_LONG);
  468.                   write_exp_elt (UNOP_MEMVAL);
  469. /*                  write_exp_elt (builtin_type_char); */
  470.                       if(misc_function_vector[i].address <= end_of_text_addr)
  471.                       write_exp_elt
  472.                           (lookup_function_type(builtin_type_int));
  473.                   else
  474.                       write_exp_elt(builtin_type_int);
  475.                   write_exp_elt (UNOP_MEMVAL);
  476.                 }
  477.                   else
  478.                 if (symtab_list == 0)
  479.                   error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  480.                 else
  481.                   error ("No symbol \"%s\" in current context.",
  482.                      copy_name ($1));
  483.                 }
  484.             }
  485.     ;
  486.  
  487. type    :    typebase
  488.     |    type '*'
  489.             { $$ = lookup_pointer_type ($1); }
  490.     ;
  491.  
  492. typebase
  493.     :    TYPENAME
  494.             { $$ = lookup_typename (copy_name ($1),
  495.                         expression_context_block, 0); }
  496.     |    STRUCT name
  497.             { $$ = lookup_struct (copy_name ($2),
  498.                           expression_context_block); }
  499.     |    UNION name
  500.             { $$ = lookup_union (copy_name ($2),
  501.                          expression_context_block); }
  502.     |    ENUM name
  503.             { $$ = lookup_enum (copy_name ($2),
  504.                         expression_context_block); }
  505.     |    UNSIGNED name
  506.             { $$ = lookup_unsigned_typename (copy_name ($2)); }
  507.     ;
  508.  
  509. name    :    NAME
  510.     |    TYPENAME
  511.     ;
  512. %%
  513.  
  514. /* Begin counting arguments for a function call,
  515.    saving the data about any containing call.  */
  516.  
  517. static void
  518. start_arglist ()
  519. {
  520.   register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
  521.  
  522.   new->next = funcall_chain;
  523.   new->arglist_len = arglist_len;
  524.   arglist_len = 0;
  525.   funcall_chain = new;
  526. }
  527.  
  528. /* Return the number of arguments in a function call just terminated,
  529.    and restore the data for the containing function call.  */
  530.  
  531. static int
  532. end_arglist ()
  533. {
  534.   register int val = arglist_len;
  535.   register struct funcall *call = funcall_chain;
  536.   funcall_chain = call->next;
  537.   arglist_len = call->arglist_len;
  538.   free (call);
  539.   return val;
  540. }
  541.  
  542. /* Free everything in the funcall chain.
  543.    Used when there is an error inside parsing.  */
  544.  
  545. static void
  546. free_funcalls ()
  547. {
  548.   register struct funcall *call, *next;
  549.  
  550.   for (call = funcall_chain; call; call = next)
  551.     {
  552.       next = call->next;
  553.       free (call);
  554.     }
  555. }
  556.  
  557. /* This page contains the functions for adding data to the  struct expression
  558.    being constructed.  */
  559.  
  560. /* Add one element to the end of the expression.  */
  561.  
  562. static void
  563. write_exp_elt (expelt)
  564.      union exp_element expelt;
  565. {
  566.   if (expout_ptr >= expout_size)
  567.     {
  568.       expout_size *= 2;
  569.       expout = (struct expression *) xrealloc (expout,
  570.                            sizeof (struct expression)
  571.                            + expout_size * sizeof (union exp_element));
  572.     }
  573.   expout->elts[expout_ptr++] = expelt;
  574. }
  575.  
  576. /* Add a string constant to the end of the expression.
  577.    Follow it by its length in bytes, as a separate exp_element.  */
  578.  
  579. static void
  580. write_exp_string (str)
  581.      struct stoken str;
  582. {
  583.   register int len = str.length;
  584.   register int lenelt
  585.     = (len + sizeof (union exp_element)) / sizeof (union exp_element);
  586.  
  587.   expout_ptr += lenelt;
  588.  
  589.   if (expout_ptr >= expout_size)
  590.     {
  591.       expout_size = max (expout_size * 2, expout_ptr + 10);
  592.       expout = (struct expression *) xrealloc (expout,
  593.                            sizeof (struct expression)
  594.                            + expout_size * sizeof (union exp_element));
  595.     }
  596.   bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
  597.   ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
  598.   write_exp_elt (len);
  599. }
  600.  
  601. /* During parsing of a C expression, the pointer to the next character
  602.    is in this variable.  */
  603.  
  604. static char *lexptr;
  605.  
  606. /* Tokens that refer to names do so with explicit pointer and length,
  607.    so they can share the storage that lexptr is parsing.
  608.  
  609.    When it is necessary to pass a name to a function that expects
  610.    a null-terminated string, the substring is copied out
  611.    into a block of storage that namecopy points to.
  612.  
  613.    namecopy is allocated once, guaranteed big enough, for each parsing.  */
  614.  
  615. static char *namecopy;
  616.  
  617. /* Current depth in parentheses within the expression.  */
  618.  
  619. static int paren_depth;
  620.  
  621. /* Nonzero means stop parsing on first comma (if not within parentheses).  */
  622.  
  623. static int comma_terminates;
  624.  
  625. /* Take care of parsing a number (anything that starts with a digit).
  626.    Set yylval and return the token type; update lexptr.
  627.    LEN is the number of characters in it.  */
  628.  
  629. /*** Needs some error checking for the float case ***/
  630.  
  631. static int
  632. parse_number (olen)
  633.      int olen;
  634. {
  635.   register char *p = lexptr;
  636.   register long n = 0;
  637.   register int c;
  638.   register int base = 10;
  639.   register int len = olen;
  640.   char *err_copy;
  641.  
  642.   extern double atof ();
  643.  
  644.   for (c = 0; c < len; c++)
  645.     if (p[c] == '.')
  646.       {
  647.     /* It's a float since it contains a point.  */
  648.     yylval.dval = atof (p);
  649.     lexptr += len;
  650.     return FLOAT;
  651.       }
  652.  
  653.   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
  654.     {
  655.       p += 2;
  656.       base = 16;
  657.       len -= 2;
  658.     }
  659.   else if (*p == '0')
  660.     base = 8;
  661.  
  662.   while (len-- > 0)
  663.     {
  664.       c = *p++;
  665.       n *= base;
  666.       if (c >= '0' && c <= '9')
  667.     n += c - '0';
  668.       else
  669.     {
  670.       if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
  671.       if (base == 16 && c >= 'a' && c <= 'f')
  672.         n += c - 'a' + 10;
  673.       else if (len == 0 && c == 'l')
  674.         ;
  675.       else
  676.         {
  677.           err_copy = (char *) alloca (olen + 1);
  678.           bcopy (lexptr, err_copy, olen);
  679.           err_copy[olen] = 0;
  680.           error ("Invalid number \"%s\".", err_copy);
  681.         }
  682.     }
  683.     }
  684.  
  685.   lexptr = p;
  686.   yylval.lval = n;
  687.   return INT;
  688. }
  689.  
  690. struct token
  691. {
  692.   char *operator;
  693.   int token;
  694.   enum exp_opcode opcode;
  695. };
  696.  
  697. static struct token tokentab3[] =
  698.   {
  699.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  700.     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  701.   };
  702.  
  703. static struct token tokentab2[] =
  704.   {
  705.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  706.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  707.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  708.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  709.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  710.     {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
  711.     {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
  712.     {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
  713.     {"++", INCREMENT, BINOP_END},
  714.     {"--", DECREMENT, BINOP_END},
  715.     {"->", ARROW, BINOP_END},
  716.     {"&&", AND, BINOP_END},
  717.     {"||", OR, BINOP_END},
  718.     {"::", COLONCOLON, BINOP_END},
  719.     {"<<", LSH, BINOP_END},
  720.     {">>", RSH, BINOP_END},
  721.     {"==", EQUAL, BINOP_END},
  722.     {"!=", NOTEQUAL, BINOP_END},
  723.     {"<=", LEQ, BINOP_END},
  724.     {">=", GEQ, BINOP_END}
  725.   };
  726.  
  727. /* Read one token, getting characters through lexptr.  */
  728.  
  729. static int
  730. yylex ()
  731. {
  732.   register int c;
  733.   register int namelen;
  734.   register int i;
  735.   register char *tokstart;
  736.  
  737.  retry:
  738.  
  739.   tokstart = lexptr;
  740.   /* See if it is a special token of length 3.  */
  741.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  742.     if (!strncmp (tokstart, tokentab3[i].operator, 3))
  743.       {
  744.     lexptr += 3;
  745.     yylval.opcode = tokentab3[i].opcode;
  746.     return tokentab3[i].token;
  747.       }
  748.  
  749.   /* See if it is a special token of length 2.  */
  750.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  751.     if (!strncmp (tokstart, tokentab2[i].operator, 2))
  752.       {
  753.     lexptr += 2;
  754.     yylval.opcode = tokentab2[i].opcode;
  755.     return tokentab2[i].token;
  756.       }
  757.  
  758.   switch (c = *tokstart)
  759.     {
  760.     case 0:
  761.       return 0;
  762.  
  763.     case ' ':
  764.     case '\t':
  765.     case '\n':
  766.       lexptr++;
  767.       goto retry;
  768.  
  769.     case '\'':
  770.       lexptr++;
  771.       c = *lexptr++;
  772.       if (c == '\\')
  773.     c = parse_escape (&lexptr);
  774.       yylval.lval = c;
  775.       c = *lexptr++;
  776.       if (c != '\'')
  777.     error ("Invalid character constant.");
  778.       return CHAR;
  779.  
  780.     case '(':
  781.       paren_depth++;
  782.       lexptr++;
  783.       return c;
  784.  
  785.     case ')':
  786.       if (paren_depth == 0)
  787.     return 0;
  788.       paren_depth--;
  789.       lexptr++;
  790.       return c;
  791.  
  792.     case ',':
  793.       if (comma_terminates && paren_depth == 0)
  794.     return 0;
  795.       lexptr++;
  796.       return c;
  797.  
  798.     case '+':
  799.     case '-':
  800.     case '*':
  801.     case '/':
  802.     case '%':
  803.     case '|':
  804.     case '&':
  805.     case '^':
  806.     case '~':
  807.     case '!':
  808.     case '@':
  809.     case '<':
  810.     case '>':
  811.     case '[':
  812.     case ']':
  813.     case '.':
  814.     case '?':
  815.     case ':':
  816.     case '=':
  817.     case '{':
  818.     case '}':
  819.       lexptr++;
  820.       return c;
  821.  
  822.     case '"':
  823.       for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
  824.     if (c == '\\')
  825.       {
  826.         c = tokstart[++namelen];
  827.         if (c >= '0' && c <= '9')
  828.           {
  829.         c = tokstart[++namelen];
  830.         if (c >= '0' && c <= '9')
  831.           c = tokstart[++namelen];
  832.           }
  833.       }
  834.       yylval.sval.ptr = tokstart + 1;
  835.       yylval.sval.length = namelen - 1;
  836.       lexptr += namelen + 1;
  837.       return STRING;
  838.     }
  839.   if (c >= '0' && c <= '9')
  840.     {
  841.       /* It's a number */
  842.       for (namelen = 0;
  843.        c = tokstart[namelen],
  844.        (c == '_' || c == '$' || c == '.' || (c >= '0' && c <= '9')
  845.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  846.        namelen++)
  847.     ;
  848.       return parse_number (namelen);
  849.     }
  850.  
  851.   if (!(c == '_' || c == '$'
  852.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  853.     error ("Invalid token in expression.");
  854.  
  855.   /* It is a name.  See how long it is.  */
  856.  
  857.   for (namelen = 0;
  858.        c = tokstart[namelen],
  859.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  860.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  861.        namelen++)
  862.     ;
  863.  
  864.   /* The token "if" terminates the expression and is NOT 
  865.      removed from the input stream.  */
  866.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  867.     {
  868.       return 0;
  869.     }
  870.  
  871.   lexptr += namelen;
  872.  
  873.   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  874.      and $$digits (equivalent to $<-digits> if you could type that).
  875.      Make token type LAST, and put the number (the digits) in yylval.  */
  876.  
  877.   if (*tokstart == '$')
  878.     {
  879.       register int negate = 0;
  880.       c = 1;
  881.       /* Double dollar means negate the number and add -1 as well.
  882.      Thus $$ alone means -1.  */
  883.       if (namelen >= 2 && tokstart[1] == '$')
  884.     {
  885.       negate = 1;
  886.       c = 2;
  887.     }
  888.       if (c == namelen)
  889.     {
  890.       /* Just dollars (one or two) */
  891.       yylval.lval = - negate;
  892.       return LAST;
  893.     }
  894.       /* Is the rest of the token digits?  */
  895.       for (; c < namelen; c++)
  896.     if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
  897.       break;
  898.       if (c == namelen)
  899.     {
  900.       yylval.lval = atoi (tokstart + 1 + negate);
  901.       if (negate)
  902.         yylval.lval = - yylval.lval;
  903.       return LAST;
  904.     }
  905.     }
  906.  
  907.   /* Handle tokens that refer to machine registers:
  908.      $ followed by a register name.  */
  909.  
  910.   if (*tokstart == '$')
  911.     for (c = 0; c < NUM_REGS; c++)
  912.       if (namelen - 1 == strlen (reg_names[c])
  913.       && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
  914.     {
  915.       yylval.lval = c;
  916.       return REGNAME;
  917.     }
  918.  
  919.   if (namelen == 6 && !strncmp (tokstart, "struct", 6))
  920.     {
  921.       return STRUCT;
  922.     }
  923.   if (namelen == 5 && !strncmp (tokstart, "union", 5))
  924.     {
  925.       return UNION;
  926.     }
  927.   if (namelen == 4 && !strncmp (tokstart, "enum", 4))
  928.     {
  929.       return ENUM;
  930.     }
  931.   if (namelen == 6 && !strncmp (tokstart, "sizeof", 6))
  932.     {
  933.       return SIZEOF;
  934.     }
  935.   if (namelen == 8 && !strncmp (tokstart, "unsigned", 6))
  936.     {
  937.       return UNSIGNED;
  938.     }
  939.   yylval.sval.ptr = tokstart;
  940.   yylval.sval.length = namelen;
  941.  
  942.   /* Any other names starting in $ are debugger internal variables.  */
  943.  
  944.   if (*tokstart == '$')
  945.     {
  946.       yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
  947.       return VARIABLE;
  948.     }
  949.  
  950.   /* Use token-type TYPENAME for symbols that happen to be defined
  951.      currently as names of types; NAME for other symbols.
  952.      The caller is not constrained to care about the distinction.  */
  953.   if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
  954.     return TYPENAME;
  955.   return NAME;
  956. }
  957.  
  958. static
  959. yyerror ()
  960. {
  961.   error ("Invalid syntax in expression.");
  962. }
  963.  
  964. /* Return a null-terminated temporary copy of the name
  965.    of a string token.  */
  966.  
  967. static char *
  968. copy_name (token)
  969.      struct stoken token;
  970. {
  971.   bcopy (token.ptr, namecopy, token.length);
  972.   namecopy[token.length] = 0;
  973.   return namecopy;
  974. }
  975.  
  976. /* Reverse an expression from suffix form (in which it is constructed)
  977.    to prefix form (in which we can conveniently print or execute it).  */
  978.  
  979. static void prefixify_subexp ();
  980.  
  981. static void
  982. prefixify_expression (expr)
  983.      register struct expression *expr;
  984. {
  985.   register int len = sizeof (struct expression) +
  986.                     expr->nelts * sizeof (union exp_element);
  987.   register struct expression *temp;
  988.   register int inpos = expr->nelts, outpos = 0;
  989.  
  990.   temp = (struct expression *) alloca (len);
  991.  
  992.   /* Copy the original expression into temp.  */
  993.   bcopy (expr, temp, len);
  994.  
  995.   prefixify_subexp (temp, expr, inpos, outpos);
  996. }
  997.  
  998. /* Return the number of exp_elements in the subexpression of EXPR
  999.    whose last exp_element is at index ENDPOS - 1 in EXPR.  */
  1000.  
  1001. static int
  1002. length_of_subexp (expr, endpos)
  1003.      register struct expression *expr;
  1004.      register int endpos;
  1005. {
  1006.   register int oplen = 1;
  1007.   register int args = 0;
  1008.   register int i;
  1009.  
  1010.   if (endpos < 0)
  1011.     error ("?error in length_of_subexp");
  1012.  
  1013.   i = (int) expr->elts[endpos - 1].opcode;
  1014.  
  1015.   switch (i)
  1016.     {
  1017.     case OP_LONG:
  1018.     case OP_DOUBLE:
  1019.       oplen = 4;
  1020.       break;
  1021.  
  1022.     case OP_VAR_VALUE:
  1023.     case OP_LAST:
  1024.     case OP_REGISTER:
  1025.     case OP_INTERNALVAR:
  1026.       oplen = 3;
  1027.       break;
  1028.  
  1029.     case OP_FUNCALL:
  1030.       oplen = 3;
  1031.       args = 1 + expr->elts[endpos - 2].longconst;
  1032.       break;
  1033.  
  1034.     case UNOP_CAST:
  1035.     case UNOP_MEMVAL:
  1036.       oplen = 3;
  1037.       args = 1;
  1038.       break;
  1039.  
  1040.     case STRUCTOP_STRUCT:
  1041.     case STRUCTOP_PTR:
  1042.       args = 1;
  1043.     case OP_STRING:
  1044.       oplen = 3 + ((expr->elts[endpos - 2].longconst
  1045.             + sizeof (union exp_element))
  1046.            / sizeof (union exp_element));
  1047.            
  1048.       break;
  1049.  
  1050.     case TERNOP_COND:
  1051.       args = 3;
  1052.       break;
  1053.  
  1054.     case BINOP_ASSIGN_MODIFY:
  1055.       oplen = 3;
  1056.       args = 2;
  1057.       break;
  1058.  
  1059.     default:
  1060.       args = 1 + (i < (int) BINOP_END);
  1061.     }
  1062.  
  1063.   while (args > 0)
  1064.     {
  1065.       oplen += length_of_subexp (expr, endpos - oplen);
  1066.       args--;
  1067.     }
  1068.  
  1069.   return oplen;
  1070. }
  1071.  
  1072. /* Copy the subexpression ending just before index INEND in INEXPR
  1073.    into OUTEXPR, starting at index OUTBEG.
  1074.    In the process, convert it from suffix to prefix form.  */
  1075.  
  1076. static void
  1077. prefixify_subexp (inexpr, outexpr, inend, outbeg)
  1078.      register struct expression *inexpr;
  1079.      struct expression *outexpr;
  1080.      register int inend;
  1081.      int outbeg;
  1082. {
  1083.   register int oplen = 1;
  1084.   register int args = 0;
  1085.   register int i;
  1086.   int *arglens;
  1087.   enum exp_opcode opcode;
  1088.  
  1089.   /* Compute how long the last operation is (in OPLEN),
  1090.      and also how many preceding subexpressions serve as
  1091.      arguments for it (in ARGS).  */
  1092.  
  1093.   opcode = inexpr->elts[inend - 1].opcode;
  1094.   switch (opcode)
  1095.     {
  1096.     case OP_LONG:
  1097.     case OP_DOUBLE:
  1098.       oplen = 4;
  1099.       break;
  1100.  
  1101.     case OP_VAR_VALUE:
  1102.     case OP_LAST:
  1103.     case OP_REGISTER:
  1104.     case OP_INTERNALVAR:
  1105.       oplen = 3;
  1106.       break;
  1107.  
  1108.     case OP_FUNCALL:
  1109.       oplen = 3;
  1110.       args = 1 + inexpr->elts[inend - 2].longconst;
  1111.       break;
  1112.  
  1113.     case UNOP_CAST:
  1114.     case UNOP_MEMVAL:
  1115.       oplen = 3;
  1116.       args = 1;
  1117.       break;
  1118.  
  1119.     case STRUCTOP_STRUCT:
  1120.     case STRUCTOP_PTR:
  1121.       args = 1;
  1122.     case OP_STRING:
  1123.       oplen = 3 + ((inexpr->elts[inend - 2].longconst
  1124.             + sizeof (union exp_element))
  1125.            / sizeof (union exp_element));
  1126.            
  1127.       break;
  1128.  
  1129.     case TERNOP_COND:
  1130.       args = 3;
  1131.       break;
  1132.  
  1133.     case BINOP_ASSIGN_MODIFY:
  1134.       oplen = 3;
  1135.       args = 2;
  1136.       break;
  1137.  
  1138.     default:
  1139.       args = 1 + ((int) opcode < (int) BINOP_END);
  1140.     }
  1141.  
  1142.   /* Copy the final operator itself, from the end of the input
  1143.      to the beginning of the output.  */
  1144.   inend -= oplen;
  1145.   bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
  1146.      oplen * sizeof (union exp_element));
  1147.   outbeg += oplen;
  1148.  
  1149.   /* Find the lengths of the arg subexpressions.  */
  1150.   arglens = (int *) alloca (args * sizeof (int));
  1151.   for (i = args - 1; i >= 0; i--)
  1152.     {
  1153.       oplen = length_of_subexp (inexpr, inend);
  1154.       arglens[i] = oplen;
  1155.       inend -= oplen;
  1156.     }
  1157.  
  1158.   /* Now copy each subexpression, preserving the order of
  1159.      the subexpressions, but prefixifying each one.
  1160.      In this loop, inend starts at the beginning of
  1161.      the expression this level is working on
  1162.      and marches forward over the arguments.
  1163.      outbeg does similarly in the output.  */
  1164.   for (i = 0; i < args; i++)
  1165.     {
  1166.       oplen = arglens[i];
  1167.       inend += oplen;
  1168.       prefixify_subexp (inexpr, outexpr, inend, outbeg);
  1169.       outbeg += oplen;
  1170.     }
  1171. }
  1172.  
  1173. /* This page contains the two entry points to this file.  */
  1174.  
  1175. /* Read a C expression from the string *STRINGPTR points to,
  1176.    parse it, and return a pointer to a  struct expression  that we malloc.
  1177.    Use block BLOCK as the lexical context for variable names;
  1178.    if BLOCK is zero, use the block of the selected stack frame.
  1179.    Meanwhile, advance *STRINGPTR to point after the expression,
  1180.    at the first nonwhite character that is not part of the expression
  1181.    (possibly a null character).
  1182.  
  1183.    If COMMA is nonzero, stop if a comma is reached.  */
  1184.  
  1185. struct expression *
  1186. parse_c_1 (stringptr, block, comma)
  1187.      char **stringptr;
  1188.      struct block *block;
  1189. {
  1190.   struct cleanup *old_chain;
  1191.  
  1192.   lexptr = *stringptr;
  1193.  
  1194.   paren_depth = 0;
  1195.   comma_terminates = comma;
  1196.  
  1197.   if (lexptr == 0 || *lexptr == 0)
  1198.     error_no_arg ("expression to compute");
  1199.  
  1200.   old_chain = make_cleanup (free_funcalls, 0);
  1201.   funcall_chain = 0;
  1202.  
  1203.   expression_context_block = block ? block : get_selected_block ();
  1204.  
  1205.   namecopy = (char *) alloca (strlen (lexptr) + 1);
  1206.   expout_size = 10;
  1207.   expout_ptr = 0;
  1208.   expout = (struct expression *) xmalloc (sizeof (struct expression)
  1209.                       + expout_size * sizeof (union exp_element));
  1210.   make_cleanup (free_current_contents, &expout);
  1211.   if (yyparse ())
  1212.     yyerror ();
  1213.   discard_cleanups (old_chain);
  1214.   expout->nelts = expout_ptr;
  1215.   expout = (struct expression *)
  1216.     xrealloc (expout,
  1217.           sizeof (struct expression)
  1218.           + expout_ptr * sizeof (union exp_element));
  1219.   prefixify_expression (expout);
  1220.   *stringptr = lexptr;
  1221.   return expout;
  1222. }
  1223.  
  1224. /* Parse STRING as an expression, and complain if this fails
  1225.    to use up all of the contents of STRING.  */
  1226.  
  1227. struct expression *
  1228. parse_c_expression (string)
  1229.      char *string;
  1230. {
  1231.   register struct expression *exp;
  1232.   exp = parse_c_1 (&string, 0, 0);
  1233.   if (*string)
  1234.     error ("Junk after end of expression.");
  1235.   return exp;
  1236. }
  1237.